package com.card.user;

import java.util.Date;
import java.util.List;

import com.card.game.ddz.DdzRoom;
import com.card.game.log.ShLog;
import com.card.game.log.ShLog.Log;
import com.card.game.log.ShResults;
import com.card.game.pdk.PdkRoom;
import com.google.common.collect.Lists;
import com.xiaoao.net.utils.TimeUtils;

/**
 * 用户额外信息
 * @author xiaoaogame
 *
 */
public class UserPlayBacks extends UserDataComponent{
	private List<RoomBack> backList = Lists.newArrayList();
	private List<RoomBack> zjhBackList = Lists.newArrayList();
	private List<RoomBack> nnBackList = Lists.newArrayList();
	private List<RoomBack> ddzBackList = Lists.newArrayList();
	private List<RoomBack> pdkBackList = Lists.newArrayList();
	
	@Override
	public void LateInit(UserBase context) {
		super.LateInit(context);
		if(backList.isEmpty()) {
			List<String> names = Lists.newArrayList();
			List<Integer> scores = Lists.newArrayList();
			List<String> uids = Lists.newArrayList();
			List<String> urls = Lists.newArrayList();
			for (int i = 0; i < 4; i++) {
				names.add("昵称"+i);
				scores.add(i);
				uids.add(i+"");
				urls.add("https://img3.doubanio.com/lpic/s26921690.jpg");
			}
			RoomBack roomBack = new RoomBack(666,"1");
			roomBack.names = names;
			roomBack.scores = scores;
			roomBack.uids = uids;
			roomBack.urls = urls;
			
			UserBack ub = new UserBack();
			ub.id = "2s";
			ub.date = TimeUtils.ToString2(new Date());
			ub.roomId = 6666;
			ub.juNum = 1;
			ub.names = names;
			ub.scores = scores;
			roomBack.backs.add(ub);
			roomBack.backs.add(ub);
			roomBack.backs.add(ub);
			roomBack.backs.add(ub);
			backList.add(roomBack);
		}
	}


	public void addUserBack(ShResults shResults,String id,int roomId,int juNum,String ownId,
			ShLog shLog) {
		RoomBack roomBack = getRoomBack(roomId);
		if(roomBack == null) {
			roomBack = new RoomBack(roomId,ownId);
			this.backList.add(roomBack);
		}
		if(this.backList.size()>5) {
			this.backList.remove(0);
		}
		UserBack ub = new UserBack();
		ub.id = id;
		ub.date = TimeUtils.ToString2(new Date());
		ub.roomId = roomId;
		ub.juNum = juNum;
		for (Log log : shLog.getScoreLogs()) {
			ub.names.add(log.getName());
			ub.scores.add(log.getScore());
		}
		roomBack.addBackList(shResults, ub);
		SetChanged();
	}
	
	public void addDdzUserBack(DdzRoom room,UserBack ub) {
		int roomId = room.getId();
		String ownId = room.getOwnerId();
		RoomBack roomBack = getDdzRoomBack(roomId);
		if(roomBack == null) {
			roomBack = new RoomBack(roomId,ownId);
			this.ddzBackList.add(roomBack);
		}
		if(this.ddzBackList.size()>5) {
			this.ddzBackList.remove(0);
		}
		roomBack.addBackList(room, ub);
		SetChanged();
	}
	
	public void addPdkUserBack(PdkRoom room,UserBack ub) {
		int roomId = room.getId();
		String ownId = room.getOwnerId();
		RoomBack roomBack = getPdkRoomBack(roomId);
		if(roomBack == null) {
			roomBack = new RoomBack(roomId,ownId);
			this.pdkBackList.add(roomBack);
		}
		if(this.pdkBackList.size()>5) {
			this.pdkBackList.remove(0);
		}
		roomBack.addBackList(room, ub);
		SetChanged();
	}
	
	public RoomBack getRoomBack(int roomId) {
		for (int i = backList.size()-1; i >= 0; i--) {
			if(backList.get(i).roomId == roomId) {
				return backList.get(i);
			}
		}
		return null;
	}
	public RoomBack getDdzRoomBack(int roomId) {
		for (int i = ddzBackList.size()-1; i >= 0; i--) {
			if(ddzBackList.get(i).roomId == roomId) {
				return ddzBackList.get(i);
			}
		}
		return null;
	}
	
	public RoomBack getPdkRoomBack(int roomId) {
		for (int i = pdkBackList.size()-1; i >= 0; i--) {
			if(pdkBackList.get(i).roomId == roomId) {
				return pdkBackList.get(i);
			}
		}
		return null;
	}
	
	public void addZjhRoomBack(RoomBack roomBack) {
		if(this.zjhBackList.size()>5) {
			this.zjhBackList.remove(0);
		}
		this.zjhBackList.add(roomBack);
		SetChanged();
	}
	
	public void addNnRoomBack(RoomBack roomBack) {
		if(this.nnBackList.size()>5) {
			this.nnBackList.remove(0);
		}
		this.nnBackList.add(roomBack);
		SetChanged();
	}
}

